package com.ubxtech.cloud.listener;

import cn.hutool.core.util.StrUtil;
import com.ubxtech.cloud.client.PushPathClient;
import com.ubxtech.cloud.config.CloudConfig;
import com.ubxtech.cloud.properties.CloudProperties;
import com.ubxtech.core.dto.RequestMappingServiceDTO;
import com.ubxtech.core.utils.StrPool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author Victor.Xiao
 * @since 2023-11-23 20:19
 **/
@Component
@ConditionalOnBean(FeignClientBuilder.class)
@AutoConfigureAfter(CloudConfig.class)
@Slf4j
public class RequestMappingServiceListener {

    @Resource
    private PushPathClient pushPathClient;
    @Resource
    private CloudProperties cloudProperties;

    @EventListener
    public void handleEvent(RequestMappingServiceDTO event) {

        if (log.isDebugEnabled()) {
            log.debug("监听到发送RequestMappingServiceDTO事件 service:{}", event.getServiceId());
        }
        try {
            if (StrUtil.isNotBlank(event.getServiceId()) && !StrPool.UNKNOW.equalsIgnoreCase(cloudProperties.getMsgServiceName()) && !event.getServiceId().equalsIgnoreCase(cloudProperties.getMsgServiceName())) {
                pushPathClient.reportPath(event);
            }
        } catch (Exception e) {
            log.error("上报path异常",e);
        }
    }
}
